package ua.kpi.apolonov.questions;

import java.util.List;
import ua.kpi.apolonov.answers.Answer;
import ua.kpi.apolonov.normalizers.AnswersNormalizers;
import ua.kpi.apolonov.parsers.Parsers;

/**
 * @version 4.0 12.03.2013
 * @author Yegor Apolonov
 */

public class TextAnswerQuestion extends Question{

	private final int TRUE_ANSWER_NUMBER = 0;
	private AnswersNormalizers normalizer = new AnswersNormalizers();
	private Parsers parser = new Parsers();

	public TextAnswerQuestion(String question, List<Answer> answers) {
		super(question, answers);
		setInquiry("\nПеречислите все правильные ответы\n>> ");
	}
	
	private String getTrueAnswerText() {
		return getAnswer(TRUE_ANSWER_NUMBER).getAnswerText();
	}
	
	public float inspectAnswers(String userTextAnswers) {
        float result = 0;
        String[] userAnswerElements = parser.parseTextAnswer(userTextAnswers);
        String[] trueAnswerElements = parser.parseTextAnswer(getTrueAnswerText());
        userAnswerElements = normalizer.textElementsNormalizer(userAnswerElements);
        trueAnswerElements = normalizer.textElementsNormalizer(trueAnswerElements);
               
        for(String singleAnswerElement : userAnswerElements) {
        	for(String trueAnswerElement : trueAnswerElements) {
        		if(singleAnswerElement.equals(trueAnswerElement)) {
        			result++;
        		}
        	}
        }
        
        result /= trueAnswerElements.length;
        
        return result;
    }
	
}
